// pages/consult/index.js
const {cate} = require('../../http/api')
const {merchantPage} = require('../../http/api')
const { upload } =  require('../../http/api')
const {
  baseUrl
} = require('../../http/env').prod
Page({

  /**
   * 页面的初始数据
   */
  data: {
    reject:false, //驳回理由
    name:'', //商户名称
    accounts: [], //商品分类
    accountsArr:[],
    accountIndex: 0,
    tempFilePaths:'', //照片
    tempFilePath:'', // 照片假
    tempFilePathsCopy:'', //反面
    tempFilePathsCopys:'', //反面假
    IDcardPaths:'', //身份证正面
    IDcardPath:'', //身份证正面假
    IDcardPathsCopy:'', //身份证反面
    IDcardPathsCopy:'', //身份证反面假
    photo:'', //手机号码
    agentNo:'', //代理商编号
    showOneButtonDialog:false, //弹窗
    oneButton: [{text: '确定'}],
    id:'', // id,
    fenleis:'请选择商户分类',
    cate_id:''
  },
  formInputChange(e){
    const field = e.currentTarget.dataset.field
    const val = e.detail.value
    if(field == 'name'){
      this.setData({
        name:val
      })
    }else if(field == 'phone' ){
      this.setData({
        photo:val
      })
    }else{
      this.setData({
        agentNo:val
      })
    }
  },
  // 商户分类
  getcate(){
    cate().then(res=>{
      const arr = res.data
      const list = []
      arr.forEach((e)=>{
        list.push(e.name)
      })
      this.setData({
        accountsArr:arr,
        accounts:list
      })
    })
  },
  bindAccountChange: function(e) {
    const value = e.detail.value
    this.setData({
        accountIndex: value,
        fenleis:this.data.accountsArr[value].name,
        cate_id:this.data.accountsArr[value].id
    })
  },
  // 上传正本
  uploadFront(v){
    const index = v.currentTarget.dataset.index
    var _this = this;
    if(index == 0){
      wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success (res) {
          _this.setData({
            tempFilePaths:res.tempFilePaths
          })
          const url = _this.data.tempFilePaths
          console.log(url)
          wx.setStorage({
            key:"key",
            data:url
          })
          wx.uploadFile({
            url:baseUrl + '/api/index/upload', 
            filePath: url[0],
            name: 'file',
            header: {
              'content-type': 'multipart/form-data'
            },
            success: res=>{
              _this.setData({
                tempFilePath:res.data
              })
              console.log(_this.data.tempFilePath)
            },
          })
        }
      })
    }else{
      wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success (res) {
          _this.setData({
            tempFilePathsCopy:res.tempFilePaths
          })
          const url = _this.data.tempFilePathsCopy
          console.log(url)
          wx.setStorage({
            key:"key",
            data:url
          })
          wx.uploadFile({
            url:baseUrl + '/api/index/upload', 
            filePath: url[0],
            name: 'file',
            header: {
              'content-type': 'multipart/form-data'
            },
            success: res=>{
              _this.setData({
                tempFilePathsCopys:res.data
              })
              console.log(_this.data.tempFilePathsCopys)
            },
          })
        }
      })
    }
  },
  // 身份证
  uploadIDcard(v){
    const index = v.currentTarget.dataset.index
    var _this = this;
    if(index == 0){
      wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success (res) {
          _this.setData({
            IDcardPaths:res.tempFilePaths
          })
          const url = _this.data.IDcardPaths
          wx.setStorage({
            key:"key1",
            data:url
          })
          wx.uploadFile({
            url:baseUrl + '/api/index/upload', 
            filePath: url[0],
            name: 'file',
            header: {
              'content-type': 'multipart/form-data'
            },
            success: res=>{
              _this.setData({
                IDcardPath:res.data
              })
              console.log(_this.data.IDcardPath)
            },
          })
        }
      })
    }else{
      wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success (res) {
          _this.setData({
            IDcardPathsCopy:res.tempFilePaths
          })
          const url = _this.data.IDcardPathsCopy
          wx.setStorage({
            key:"key2",
            data:url
          })
          wx.uploadFile({
            url:baseUrl + '/api/index/upload', 
            filePath: url[0],
            name: 'file',
            header: {
              'content-type': 'multipart/form-data'
            },
            success: res=>{
              _this.setData({
                IDcardPathsCopys:res.data
              })
              console.log(_this.data.IDcardPathsCopys)
            },
          })
        }
      })
    }
  },
  // 提交按钮
  btnSubmit(){
    if(this.data.cate_id==''){
      wx.showToast({
        title: '请选择商户分类',
        icon:'none',
        duration:1500
      })
      return
    } 
    if(this.data.IDcardPathsCopy==''){
      wx.showToast({
        title: '请上传身份证反面',
        icon:'none',
        duration:1500
      })
      return
    }
    if(this.data.tempFilePathsCopy==''){
      wx.showToast({
        title: '请上传营业执照副本',
        icon:'none',
        duration:1500
      })
      return
    }
    var obj = {
      name:this.data.name,
      cate_id:this.data.cate_id,
      zhizhao:this.data.tempFilePath,
      zhizhao_back:this.data.tempFilePathsCopys,
      card_front:this.data.IDcardPath,
      card_back:this.data.IDcardPathsCopys,
      mobile:this.data.photo,
      re_code:this.data.agentNo
    }
    console.log(obj)
    merchantPage(obj).then(res=>{
      if(res.code == 200){
        wx.showToast({
          title: res.msg,
          icon:'none',
          duration:1500
        })
        setTimeout(function(){
          wx.navigateTo({
            url: '../agreement/index?id='+res.id,
          })
        },1500)
        
      }else{
        wx.showToast({
          title: res.msg,
          icon:'none',
          duration:1500
        })
      }
    })
    this.setData({
      reject:this.reject,
      //showOneButtonDialog:true,
    })
  },
  //确定按钮
  tapDialogButton(){
    const id = this.data.id
    console.log(id)
    wx.navigateTo({
      url: '../agreement/index?id='+id,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getcate()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})